package com.hdkj.serviceai.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hdkj.serviceai.conutils.ConUtil ;
import com.hdkj.serviceai.service.TrainService;
import com.hdkj.serviceai.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TrainServiceImpl implements TrainService {
    @Override
    public Object gettraininfo(Map<String, Object> map) {
        String host = "https://jisutrain.market.alicloudapi.com";
        String path = "/train/station2s";
        String method = "get";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + ConUtil.getkey());
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, Object> querys = new HashMap<String, Object>();
        querys.put("date", map.get("date"));
        querys.put("end", map.get("end"));
        querys.put("ishigh", map.get("ishigh"));
        querys.put("start", map.get("start"));
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);

            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity(), "utf-8"));

            return jsonObject.get("result");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
